package aaa;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/aaa")
public class a extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        Cookie cookie = new Cookie("aaa", "123");
        cookie.setMaxAge(-1);//表示cookie的有效期<0，cookie不会存储（表示不会被存储到硬盘文件中，会放在浏览器运行内存当中）
        cookie.setPath("/servlet13");//只要是这个Servlet13项目下的请求路径，都会提交这个cookie给服务器
        resp.addCookie(cookie);
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie1 : cookies) {
                String name = cookie1.getName();
                String value = cookie1.getValue();
                System.out.println(name + ":" + value);
            }
        }
    }
}